package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.RegionQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("region")
public class RegionController {
    @Autowired
    private IRegionService regionService;

    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")RegionQuery qo) {
        Page<Region> page = regionService.query(qo);
        model.addAttribute("page", page);
        System.out.println(page.getContent());
        return "region/list";
    }

    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(int deep) {

        //deep: 1.国家 2.省份 3.城市
        //如果需要使用deep作为条件： <= deep
//        return destinationService.queryDestByDeep(deep);

        return destinationService.list();
    }

//    StringUtils.hasLength(region.getId())

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region) {
        if (region != null) {
            regionService.update(region);
        } else {
            regionService.insert(region);
        }
        return JsonResult.success();
    }

    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid) {
        List<Destination> destinations = destinationService.getDestByRegionId(rid);
        return destinations;
    }

    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(String id, int hot) {
        regionService.changeHotValue(id, hot);
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        regionService.delete(id);
        return JsonResult.success();
    }
}
